package com.jinglin.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jinglin.controller.ABaseController;
import com.jinglin.entity.dto.TokenUserInfoDto;
import com.jinglin.entity.enums.ResponseCodeEnum;
import com.jinglin.entity.po.UserVideoSeries;
import com.jinglin.entity.po.UserVideoSeriesVideo;
import com.jinglin.entity.po.VideoInfo;
import com.jinglin.entity.vo.ResponseVO;
import com.jinglin.entity.vo.UserVideoSeriesDetailVO;
import com.jinglin.exception.BusinessException;
import com.jinglin.mapper.UserVideoSeriesMapper;
import com.jinglin.mapper.UserVideoSeriesVideoMapper;
import com.jinglin.mapper.VideoInfoMapper;
import com.jinglin.service.UserVideoSeriesService;
import com.jinglin.service.UserVideoSeriesVideoService;
import com.jinglin.service.VideoInfoService;
import com.jinglin.web.annotation.GlobalInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Size;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户视频序列归档 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2025-03-30
 */
@RestController
@RequestMapping("/uhome/series")
public class UserVideoSeriesController extends ABaseController {
    @Autowired
    private UserVideoSeriesService userVideoSeriesService;
    @Autowired
    private UserVideoSeriesMapper userVideoSeriesMapper;
    @Autowired
    private VideoInfoMapper videoInfoMapper;
    @Autowired
    private VideoInfoService videoInfoService;
    @Autowired
    private UserVideoSeriesVideoService userVideoSeriesVideoService;
    @Autowired
    private UserVideoSeriesVideoMapper userVideoSeriesVideoMapper;
    //加载视频合集列表
    @RequestMapping("/loadVideoSeries")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO loadVideoSeries(@NotEmpty String userId){
        // 将视频合集中最先存入的视频的封面，作为合集封面
        List<UserVideoSeries> list = userVideoSeriesMapper.getSeriesList(userId);
        return getSuccessResponseVO(list);
    }
    // 保存/更新视频合集
    @RequestMapping("/saveVideoSeries")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO saveVideoSeries(String seriesId,
                                      @NotEmpty @Size(max = 100)String seriesName,
                                      @Size(max = 200)String seriesDescription,
                                      String videoIds){
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        UserVideoSeries userVideoSeries = new UserVideoSeries();
        userVideoSeries.setSeriesId(seriesId);
        userVideoSeries.setSeriesName(seriesName);
        userVideoSeries.setSeriesDescription(seriesDescription);
        userVideoSeries.setUserId(tokenUserInfoDto.getUserId());
        userVideoSeriesService.saveUserVideoSeries(userVideoSeries,videoIds);
        return getSuccessResponseVO(null);
    }

    @RequestMapping("/delVideoSeries")
    @Transactional
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO delVideoSeries (String seriesId) {
        LambdaQueryWrapper<UserVideoSeries> seriesWrapper = new LambdaQueryWrapper<>();
        seriesWrapper.eq(UserVideoSeries::getSeriesId,seriesId);
        userVideoSeriesService.remove(seriesWrapper);

        // 再删合集里面的视频信息
        LambdaQueryWrapper<UserVideoSeriesVideo> seriesVideoWrapper = new LambdaQueryWrapper<>();
        seriesVideoWrapper.eq(UserVideoSeriesVideo::getSeriesId,seriesId);
        userVideoSeriesVideoService.remove(seriesVideoWrapper);
        return getSuccessResponseVO(null);
    }

    // 保存/更新视频合集
    @RequestMapping("/saveSeriesVideo")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO saveSeriesVideo(String seriesId,
                                      String videoIds){
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        UserVideoSeries userVideoSeries = new UserVideoSeries();
        userVideoSeries.setSeriesId(seriesId);
        userVideoSeries.setUserId(tokenUserInfoDto.getUserId());
        userVideoSeriesService.saveSeriesVideo(userVideoSeries,videoIds);
        return getSuccessResponseVO(null);
    }

    @RequestMapping("/delSeriesVideo")
    @Transactional
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO delSeriesVideo (@NotEmpty String seriesId,@NotEmpty String videoId) {
        LambdaQueryWrapper<UserVideoSeriesVideo> seriesVideoWrapper = new LambdaQueryWrapper<>();
        seriesVideoWrapper.eq(UserVideoSeriesVideo::getSeriesId,seriesId);
        seriesVideoWrapper.eq(UserVideoSeriesVideo::getVideoId,videoId);
        userVideoSeriesVideoService.remove(seriesVideoWrapper);
        return getSuccessResponseVO(null);
    }


    @RequestMapping("/loadAllVideo")
    public ResponseVO loadAllVideo(String seriesId){
        // 我们要将所有本用户的视频信息全部返回（不包括已经在当前合集的视频,一个视频可以在多个合集中）
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        LambdaQueryWrapper<UserVideoSeriesVideo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserVideoSeriesVideo::getUserId,tokenUserInfoDto.getUserId());
        wrapper.eq(UserVideoSeriesVideo::getSeriesId,seriesId);
        List<UserVideoSeriesVideo> list = userVideoSeriesVideoService.list(wrapper);
        List<String> ids = list.stream().map(UserVideoSeriesVideo::getVideoId).collect(Collectors.toList());


        List<VideoInfo> allVideoExclude = videoInfoMapper.getAllVideoExclude(ids,tokenUserInfoDto.getUserId());
        return getSuccessResponseVO(allVideoExclude);
    }


    @RequestMapping("/getVideoSeriesDetail")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO getVideoSeriesDetail(@NotEmpty String  seriesId) {
        UserVideoSeries videoSeries = userVideoSeriesService.getById(seriesId);
        if (videoSeries == null) {
            throw new BusinessException(ResponseCodeEnum.CODE_404);
        }
        List<UserVideoSeriesVideo> seriesVideoList = userVideoSeriesVideoMapper.getSeriesDetailList(seriesId);
        return getSuccessResponseVO(new UserVideoSeriesDetailVO(videoSeries, seriesVideoList));
    }

    @RequestMapping("/loadVideoSeriesWithVideo")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO loadVideoSeriesWithVideo(@NotEmpty String userId) {
        LambdaQueryWrapper<UserVideoSeries> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserVideoSeries::getUserId,userId);
        wrapper.orderByAsc(UserVideoSeries::getSort);

        List<UserVideoSeries> videoSeriesList = userVideoSeriesService.list(wrapper);
        for (UserVideoSeries videoSeries : videoSeriesList) {
            LambdaQueryWrapper<UserVideoSeriesVideo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(UserVideoSeriesVideo::getSeriesId,videoSeries.getSeriesId());
            List<UserVideoSeriesVideo> list = userVideoSeriesVideoService.list(queryWrapper);

            List<String> videoIds = list.stream().map(UserVideoSeriesVideo::getVideoId).collect(Collectors.toList());
            if (!videoIds.isEmpty()){
                List<VideoInfo> videoInfos = videoInfoService.listByIds(videoIds);

                videoSeries.setVideoInfoList(videoInfos);
            }
            videoSeries.setVideoInfoList(Collections.emptyList());
        }
        return getSuccessResponseVO(videoSeriesList);
    }



}

